import {
  main
} from './utils/action.js'
import util from './utils/util.js'
import api from './request/api.js'
import { $store } from './store/store.js'
import { $cache } from './store/cache.js'
import { $event } from './event/event'

const keepComponent = Component
wx.newPage = function(e,name) {
  let init_data = {
    loading: false,
    pageMore: false,
    pageHave: false,
  }
  if(e.data){
    for (let i in init_data) {
      if (!e.data[i]) {
        e.data[i] = init_data[i]
      }
    }
  }else{
    e.data = init_data
  }
  e.$mixin = e.onLoad;
  e.onLoad = function(data){
    //前面可以塞入全局方法
    e.$mixin.apply(this,[data])
  }
  return Page({
    app: getApp(),
    api,
    $store,
    $cache,
    $event,
    ...main,
    util,
    ...e,
  })
}

Component = function (options, ...args) {
  const created = options.created;
  options.created = function () {
    this.$store = $store;
    this.$cache = $cache;
    this.$event = $event;
    this.api = api;
    this.util = util
    for(let item in main){
      this[item] = main[item]
    }
    // this.$watcher = new Watcher(this);
    // this.$page = wx.$page();
    created && created.bind(this)(...arguments);
  };
  keepComponent(options, ...args);
}